package com.atfusu.controller;

import com.atfusu.base.BaseController;
import com.atfusu.bean.entity.Community;
import com.atfusu.bean.entity.Dict;
import com.atfusu.service.CommunityService;
import com.atfusu.service.DictService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Title:
 * Author: fusu
 * DateTime:  2022/11/28
 * Description:
 **/
@Controller
@RequestMapping("/community")
public class communityController extends BaseController {

    private static final String PAGE_INDEX = "community/index";
    private static final String PAGE_CREATE = "community/create";
    private static final String PAGE_SUCCESS = "common/success";
    private static final String PAGE_EDIT = "community/edit";
    @DubboReference
    private DictService dictService;

    @DubboReference
    private CommunityService communityService;

    @RequestMapping
    public String findPage(HttpServletRequest request, Map map) {
        //获取分页信息及查询信息
        Map<String, Object> filters = getFilters(request);
        //调用方法完成分页及查询
        PageInfo<Community> page = communityService.findPage(filters);
        //获取数据字典中的北京地区的所有区域及板块信息
        List<Dict> areaList = dictService.findListByDictCode("beijing");


        //将数据放入请求域
        map.put("page", page);
        map.put("filters", filters);
        map.put("areaList", areaList);
        return PAGE_INDEX;
    }

    @RequestMapping("/create")
    public String create(Map map) {
        //获取数据字典中的北京地区的所有区域及板块信息
        List<Dict> areaList = dictService.findListByDictCode("beijing");

        map.put("areaList", areaList);
        return PAGE_CREATE;
    }

    @RequestMapping("/save")
    public String save(Community community) {
        communityService.insert(community);
        return PAGE_SUCCESS;
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        communityService.delete(id);

        return "redirect:/community";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Map map) {
        //获取社区信息
        Community community = communityService.getById(id);
        //获取数据字典信息
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        //请求域
        map.put("community", community);
        map.put("areaList", areaList);
        return PAGE_EDIT;
    }

    @RequestMapping("/update")
    public String update(Community community) {
        //进行修改操作
        communityService.update(community);

        return PAGE_SUCCESS;
    }
}
